﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Windows.Forms;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using CarolLib.Control;
using CarolLib.Extension;
using CarolLib.FTP;
using CarolLib.WinService;
using MM.Configuration;

namespace MMMonitor
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
            //Init();
        }
        private void frmMain_Load(object sender, EventArgs e)
        {
            BindServices();
        }

        private void Init()
        {
            //IUnityContainer myContainer = new UnityContainer();
            //UnityConfigurationSection section
            //  = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
            //section.Containers["Data"].Configure(myContainer);
        }

        private void frmMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void BindServices()
        {
            List<ServiceEntity> services = ServiceConfiguration.GetConfig().services;
            services.ForEach(s => s.Status = ServiceControl.GetServiceStatus(s.ServiceName));
            dgvSerivces.DataSource = services;
            this.dgvSerivces.Refresh();
            dgvSerivces_SelectionChanged(this, new EventArgs());
        }
        private void dgvSerivces_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvSerivces.SelectedRows.Count > 0)
            {
                string serviceName = dgvSerivces.SelectedRows[0].Cells["ServiceName"].Value.ToString();
                ServiceStatus status = (ServiceStatus)dgvSerivces.SelectedRows[0].Cells["Status"].Value;
                foreach (Control item in groupBox1.Controls)
                {
                    if (item is Button)
                    {
                        item.Enabled = false;
                    }
                }

                btnOpenlocation.Enabled = true;
                switch (status)
                {
                    case ServiceStatus.NotInstalled:
                        btnInstall.Enabled = true;
                        btnDebug.Enabled = true;
                        break;
                    case ServiceStatus.Stopped:
                        btnUninstall.Enabled = true;
                        btnStart.Enabled = true;
                        btnDebug.Enabled = true;
                        break;
                    case ServiceStatus.StartPending:
                        break;
                    case ServiceStatus.StopPending:
                        break;
                    case ServiceStatus.Running:
                        btnStop.Enabled = true;
                        break;
                    case ServiceStatus.ContinuePending:
                        break;
                    case ServiceStatus.PausePending:
                        break;
                    case ServiceStatus.Paused:
                        btnStart.Enabled = true;
                        break;
                    default:
                        break;
                }
            }
        }

        private void btnServiceControl_Click(object sender, EventArgs e)
        {
            string serviceName = dgvSerivces.SelectedRows[0].Cells["ServiceName"].Value.ToString();
            string path = ServiceConfiguration.GetConfig().services.Where(s => s.ServiceName == serviceName).First().FullPath;

            int tag = (sender as Button).Tag.ToString().TryToInt();
            switch (tag)
            {
                case 1:
                    System.Diagnostics.Process.Start(path, "-start");
                    break;
                case 2:
                    System.Diagnostics.Process.Start(path, "-stop");
                    break;
                case 3:
                    System.Diagnostics.Process.Start(path, "-install");
                    break;
                case 4:
                    System.Diagnostics.Process.Start(path, "-uninstall");
                    break;
                case 5:
                    System.Diagnostics.Process.Start(path, "-debug");
                    break;
                case 6:
                    string filepath = ServiceConfiguration.GetConfig().services.Where(s => s.ServiceName == serviceName).First().Path;
                    System.Diagnostics.Process.Start("Explorer.exe", filepath);
                    break;
                default:
                    break;
            }
        }
    }
}
